package membertask

import (
	"context"
	"strings"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/enum/errorcode"
	"mall/service/backend/common/errorx"
	"mall/service/setting/rpc/settingclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateMemberTaskLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateMemberTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMemberTaskLogic {
	return &UpdateMemberTaskLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateMemberTaskLogic) UpdateMemberTask(req *types.UpdateMemberTaskReq) (resp *types.BaseMsgResp, err error) {
	if req.Name != nil && len(strings.TrimSpace(*req.Name)) == 0 {
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "setting.memberTaskNameRequired"))
	}
	if req.Slug != nil && len(strings.TrimSpace(*req.Slug)) == 0 {
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "setting.memberTaskSlugRequired"))
	}

	res, err := l.svcCtx.SettingRpc.BackendUpdateMemberTask(l.ctx, &settingclient.MemberTaskInfo{
		Id:          req.Id,
		Name:        req.Name,
		Slug:        req.Slug,
		Description: req.Description,
		Remark:      req.Remark,
		PicId:       req.PicId,
		PicUrl:      req.PicUrl,
		Gems:        req.Gems,
		Growth:      req.Growth,
		Integrals:   req.Integrals,
		Sort:        req.Sort,
		Status:      req.Status,
	})
	if err != nil {
		return
	}

	return &types.BaseMsgResp{
		Success: true,
		Msg:     l.svcCtx.Trans.Trans(l.ctx, res.Msg),
	}, nil
}
